package matching;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import control.ProcessedInput;
import control.ProcessedOutput;

public class AimlParser {
	// input aiml file
	final String inputFile = "aiml.aiml";

	// data set of interactive human
	private Map<String, String> human;

	// constructor
	private AimlParser() {
		human = new HashMap<String, String>();
	}

	// singleton
	private static AimlParser instance = null;

	public static AimlParser getInstance() {
		if (null == instance) {
			instance = new AimlParser();
		}
		return instance;
	}

	public void setHumanProperty(String attr, String value) {
		human.put(attr, value);
	}

	public String getHumanProperty(String attr) {
		return human.get(attr);
	}

	public String getBotProperty(String attr) {
		return MyBot.name;
	}

	// -----------------------------------

	public ProcessedOutput getAnswers(ProcessedInput keys) throws IOException {

		ProcessedOutput answer = new ProcessedOutput();
		String response = "";

		// learn
		if (keys.isLearning()) {
			// TO-DO
			// learning here
			learning(null, null, null);

			// store and post answer
			response = "Thank you, Can you tell me more?";
			answer.setExist(true);
			answer.setResponse(response);
		} else {
			// that sentence
			// match input
			response = getResponse(keys.getInput());

			if (!response.equals("")) {
				answer.setExist(true);
				answer.setResponse(response);
			} else {
				// we must learn
				response = "I don't know about it. Can you explain it to me ?";
				answer.setExist(false);
				answer.setResponse(response);
			}
		}
		return answer;
	}

	public String processTemplate(String input) {
		// get first child
		String first = "";
		String template = input;
		if (first.equals(KeyTags.RANDOM)) {
			// get children li
			// choose 1 sub template randomly;
			// set template = sub template;;;
		}
		/*
		 * process template 0. check if exist srai 1. partition into 3 parts
		 * <srai> 2. part1 + part 3 -> sub template: bot, set, get 3. srai ->
		 * star -> * -> getResponse 4. remove <think>, <set name = it> 5.
		 * replace name
		 */

		return "";
	}

	private String getResponse(String input) throws IOException {
		// patterns

		// pattern matching

		// get template

		return "";
	}

	public void XMLRead(String sentence) {
		SAXBuilder builder = new SAXBuilder();
		File xmlFile = new File(inputFile);

		try {

			Document document = (Document) builder.build(xmlFile);
			Element aimlNode = document.getRootElement();
			
			//find list of categories
			List list = aimlNode.getChildren(KeyTags.CATEGORY);

			
			//for each input string
			for (int i = 0; i < list.size(); i++) {

				Element categoryNode = (Element) list.get(i);

				String patternString = categoryNode.getChildText(KeyTags.PATTERN);
				
				if (sentence.matches(patternString)) {
					
					return;	
				}
				
				System.out.println("First Name : "
						+ node.getChildText("firstname"));
				System.out.println("Last Name : "
						+ node.getChildText("lastname"));
				System.out.println("Nick Name : "
						+ node.getChildText("nickname"));
				System.out.println("Salary : " + node.getChildText("salary"));

			}

		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}
	}

	private void learning(String pattern, String response, String that) {
		SAXBuilder builder = new SAXBuilder();
		File xmlFile = new File(inputFile);

		try {
			Document document = (Document) builder.build(xmlFile);
			Element aimlNode = document.getRootElement();
					
			Element pElement = new Element("pattern").setText(pattern);
			Element thatElement = new Element("that").setText(that);
			Element tElement = new Element("template").setText(response);
			Element cElement = new Element("category");
			cElement.addContent(pElement);		
			if(!that.equals("")) cElement.addContent(thatElement);
			cElement.addContent(tElement);
			
			aimlNode.addContent(cElement);
			
			XMLOutputter xmlOutput = new XMLOutputter();

			xmlOutput.setFormat(Format.getPrettyFormat());
			xmlOutput.output(document, new FileWriter(xmlFile));	

		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}
	}
}
